package chooongg.base.fragment

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.fragment.app.Fragment
import chooongg.base.BuildConfig
import chooongg.base.QuickPageInitialize
import chooongg.base.R
import chooongg.base.annotation.Title
import chooongg.base.utils.ThrowableUtils
import chooongg.base.utils.toast
import chooongg.base.utils.toastError

/**
 * 注意：不要直接继承QuickFragment，请自行封装Base类
 */
@Title
abstract class QuickFragment : Fragment(), QuickPageInitialize {
    private var title: CharSequence = "未指定"
    fun setTitle(title: CharSequence) = apply {
        this.title = title
    }

    open fun getTitle(): CharSequence = title
    private var container: ViewGroup? = null

    private var rootView: View? = null

    var isFirstLoad = true
        private set

    abstract fun lazyLoad()

    /**
     * 重复展示时调用
     */
    open fun onReselected() = Unit

    /**
     * 更新数据
     */
    open fun update(type: Int, data: Any? = null) = Unit

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        this.container = container
        val contentLayout = getContentLayout() ?: return null
        return try {
            inflater.inflate(contentLayout, container, false)
                .apply { this@QuickFragment.rootView = this }
        } catch (e: Exception) {
            e.printStackTrace()
            val view = inflater.inflate(R.layout.activity_quick_catch, container, false)
            if (BuildConfig.DEBUG) {
                view.findViewById<TextView>(R.id.tv_title)?.text = e.message
                view.findViewById<TextView>(R.id.tv_message)?.apply {
                    text = ThrowableUtils.getFullStackTrace(e)
                    visibility = View.VISIBLE
                }
            } else {
                view.findViewById<TextView>(R.id.tv_title)?.text = "页面初始化异常"
            }
            null
        }
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        if (rootView != null) {
            try {
                val background = getWindowBackgroundRes()
                if (background != null) {
                    rootView?.setBackgroundResource(background)
                }
                initConfig(savedInstanceState)
                try {
                    initContent(savedInstanceState)
                } catch (e: Exception) {
                    if (BuildConfig.DEBUG) {
                        toast(e.toString())
                    } else {
                        toastError("出现异常\n碎片加载失败")
                    }
                }
            } catch (e: Exception) {
                e.printStackTrace()
                if (BuildConfig.DEBUG) {
                    toast(e.toString())
                } else {
                    toastError("出现异常\n碎片配置失败")
                }
            }
        }


    }

    override fun onResume() {
        super.onResume()
        if (isFirstLoad) {
            isFirstLoad = false
            lazyLoad()
        }
    }

}